﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TIS.DAO;
using System.Data;

namespace TIS.Administrator
{
    public partial class ManagerSetting : System.Web.UI.Page
    {
        SettingDAO _settingDao = new SettingDAO();
        LanguageDAO _languageDao = new LanguageDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["langId"] == null)
                {
                    Session["langId"] = 1;
                }
                loadForm();
                GetDataDdlLanguage();
            }
        }

        public void GetDataDdlLanguage()
        {

            ddlLanguage.DataSource = _languageDao.GetAllLanguage();
            ddlLanguage.DataTextField = "LanguageName";
            ddlLanguage.DataValueField = "LanguageId";
            ddlLanguage.SelectedValue = Session["langId"].ToString();
            ddlLanguage.DataBind();
        }
        public void loadForm()
        {
            DataTable dt = _settingDao.GetSetting(int.Parse(Session["langId"].ToString()));
            foreach (DataRow row in dt.Rows)
            {
                if (row["SettingKey"].ToString() == "description")
                {
                    txtDescription.Text = row["SettingValue"].ToString();
                }
                if (row["SettingKey"].ToString() == "keywords")
                {
                    txtKeyword.Text = row["SettingValue"].ToString();
                }
                if (row["SettingKey"].ToString() == "title")
                {
                    txtTitle.Text = row["SettingValue"].ToString();
                }
                if (row["SettingKey"].ToString() == "fax")
                {
                    txtFax.Text = row["SettingValue"].ToString();
                }
                //------------------
                if (row["SettingKey"].ToString() == "address")
                {
                    txtAddress.Text = row["SettingValue"].ToString();
                }
                if (row["SettingKey"].ToString() == "company")
                {
                    txtCompany.Text = row["SettingValue"].ToString();
                }
                if (row["SettingKey"].ToString() == "email")
                {
                    txtEmail.Text = row["SettingValue"].ToString();
                }
                if (row["SettingKey"].ToString() == "facebook")
                {
                    txtFacebook.Text = row["SettingValue"].ToString();
                }
                if (row["SettingKey"].ToString() == "googleplus")
                {
                    txtGooglePlus.Text = row["SettingValue"].ToString();
                }
                if (row["SettingKey"].ToString() == "hotline")
                {
                    txtHotLine.Text = row["SettingValue"].ToString();
                }
                if (row["SettingKey"].ToString() == "phone")
                {
                    txtPhone.Text = row["SettingValue"].ToString();
                }
                //-------------
                if (row["SettingKey"].ToString() == "skype")
                {
                    txtSkype.Text = row["SettingValue"].ToString();
                }
                if (row["SettingKey"].ToString() == "website")
                {
                    txtWebsite.Text = row["SettingValue"].ToString();
                }
                if (row["SettingKey"].ToString() == "yahoo")
                {
                    txtYahoo.Text = row["SettingValue"].ToString();
                }
                if (row["SettingKey"].ToString() == "youtube")
                {
                    txtYoutube.Text = row["SettingValue"].ToString();
                }
                if (row["SettingKey"].ToString() == "youtubeDescription")
                {
                    txtYoutubeDescription.Text = row["SettingValue"].ToString();
                }
                
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                int langId = int.Parse(Session["langId"].ToString());
                _settingDao.UpdateSetting("address", txtAddress.Text, langId);
                _settingDao.UpdateSetting("company", txtCompany.Text, langId);
                _settingDao.UpdateSetting("description", txtDescription.Text, langId);
                _settingDao.UpdateSetting("email", txtEmail.Text, langId);
                _settingDao.UpdateSetting("facebook", txtFacebook.Text, langId);
                _settingDao.UpdateSetting("fax", txtFax.Text, langId);
                _settingDao.UpdateSetting("googleplus", txtGooglePlus.Text, langId);
                _settingDao.UpdateSetting("hotline", txtHotLine.Text, langId);
                _settingDao.UpdateSetting("keywords", txtKeyword.Text, langId);
                _settingDao.UpdateSetting("phone", txtPhone.Text, langId);
                _settingDao.UpdateSetting("skype", txtSkype.Text, langId);
                _settingDao.UpdateSetting("title", txtTitle.Text, langId);
                _settingDao.UpdateSetting("website", txtWebsite.Text, langId);
                _settingDao.UpdateSetting("yahoo", txtYahoo.Text, langId);
                _settingDao.UpdateSetting("youtube", txtYoutube.Text, langId);
                _settingDao.UpdateSetting("youtubeDescription", txtYoutubeDescription.Text, langId);
                Response.Write("<script> alert('Cập nhập thay đổi thành công'); </script>");
            }
            catch (Exception)
            {
                Response.Write("<script> alert('Có lỗi trong quá trình sử lý'); </script>");
            }

        }

        protected void btnShowLanguage_Click(object sender, EventArgs e)
        {
            Session["langId"] = ddlLanguage.SelectedValue;
            Response.Redirect("ManagerSetting.aspx");
        }


    }
}